package com.klhappy.houyuan.utils.Multipart;

import android.content.Context;
import android.util.Log;

import com.android.volley.*;
import com.android.volley.toolbox.*;

import java.io.File;
import java.lang.ref.SoftReference;
import java.util.HashMap;

public class HttpVolleyManager {

    public static final String TAG = HttpVolleyManager.class.getSimpleName();
    /**
     * Singleton instance {@link #NetManager}
     */
    private static HttpVolleyManager instance = new HttpVolleyManager();

    private HashMap<String, String> headers = null;
    private HashMap<String, String> cookie = null;

    /**
     * RequestQueue of volley {@link RequestQueue}
     */
    private RequestQueue requestQueue = null;
    private HashMap<String, SoftReference<Request<String>>> requestMap;

    private RetryPolicy retryPolicy;

    /** The default socket timeout in milliseconds */
    private static final int DEFAULT_TIMEOUT_MS = 20000;

    /** The default number of retries */
    private static final int DEFAULT_MAX_RETRIES = 2;

    /** The default backoff multiplier */
    private static final float DEFAULT_BACKOFF_MULT = 1f;

    /**
     * Get Singleton instance {@link #instance}
     */
    public static HttpVolleyManager getInstance() {
        return instance;
    }

    /**
     * Constructors {@link #instance}
     */
    private HttpVolleyManager() {
    }

    public HashMap<String, String> getHeaders() {
        return headers;
    }

    public HashMap<String, String> getCookie() {
        if (cookie == null) {
            cookie = new HashMap<String, String>();
        }
        return cookie;
    }

    /**
     * Creates a {@link RequestQueue}.
     * 
     * @param context
     *            A {@link Context} to use for creating the cache dir.
     */
    public void init(Context context) {
        if (requestQueue == null) {
            requestQueue = Volley.newRequestQueue(context);
            retryPolicy = new DefaultRetryPolicy(DEFAULT_TIMEOUT_MS,
                    DEFAULT_MAX_RETRIES, DEFAULT_BACKOFF_MULT);
            requestMap = new HashMap<String, SoftReference<Request<String>>>();
        }
    }

    public  void doFilePost(String url, File file, String mimeType,
            final HashMap<String, String> params,
            Response.Listener<String> listener,
            Response.ErrorListener errorListener) {
        MultipartRequest postRequest = new MultipartRequest(url, errorListener,
                listener, file, mimeType, params);
        postRequest.setRetryPolicy(retryPolicy);
        Log.e("HTTPMANAGE", "POST TAG :" + url);
        postRequest.setTag(url);
        requestQueue.add(postRequest);
        requestMap.put(url, new SoftReference<Request<String>>(postRequest));
    }

    // public void doPost(String url, final HashMap<String, String> params,
    // Response.Listener<String> listener,
    // Response.ErrorListener errorListener) {
    // if (requestQueue == null) {
    // return;
    // }
    //
    // final Map<String, String> param = new HashMap<String, String>();
    // if (params != null) {
    // Iterator<Entry<String, String>> iter = params.entrySet().iterator();
    // while (iter.hasNext()) {
    // HashMap.Entry<String, String> entry = (HashMap.Entry<String, String>)
    // iter
    // .next();
    // param.put(entry.getKey(), String.valueOf(entry.getValue()));
    // }
    // }
    //
    // LoanlyRequest postRequest = new LoanlyRequest(Request.Method.POST, url,
    // listener, errorListener) {
    // @Override
    // protected Map<String, String> getParams() {
    // return param;
    // }
    // };
    //
    // postRequest.setRetryPolicy(retryPolicy);
    // Log.e("HTTPMANAGE", "POST TAG :" + url);
    // postRequest.setTag(url);
    // requestQueue.add(postRequest);
    // requestMap.put(url, new SoftReference<Request<String>>(postRequest));
    // }

    // private void doPostJson(String url, final String jsonParam,
    // Response.Listener<String> listener,
    // Response.ErrorListener errorListener) {
    // if (requestQueue == null) {
    // return;
    // }
    // LoanlyJsonRequest postRequest = new LoanlyJsonRequest(
    // Request.Method.POST, url, jsonParam, listener, errorListener);
    // postRequest.setRetryPolicy(retryPolicy);
    // Log.e("HTTPMANAGE", "POST TAG :" + url);
    // postRequest.setTag(url);
    // requestQueue.add(postRequest);
    // requestMap.put(url, new SoftReference<Request<String>>(postRequest));
    // }

    // public void doPostRpc(String strClass, String strMethod,
    // HashMap<String, String> param, Response.Listener<String> listener,
    // Response.ErrorListener errorListener) {
    // final CallBean bean = new CallBean();
    // final Functions fun = new Functions();
    // final String sessionKey = UserInfoHelper.isLogon() ? UserInfoHelper
    // .getUser().getsession_key() : "0";
    // final String userID = UserInfoHelper.isLogon() ? UserInfoHelper
    // .getUserID() : "0";
    // final Session session = new Session(sessionKey, userID);
    // LogUtil.e(TAG, strClass + "/" + strMethod + "/"
    // + (param != null ? param.size() : 0));
    // fun.className = strClass;
    // fun.method = strMethod;
    // fun.param = param;
    // bean.functions.add(fun);
    // bean.session = session;
    // LogUtil.e(
    // TAG,
    // strClass + "/" + strMethod + "/  "
    // + XmlAndJsonUtil.beanToJson(bean));
    // doPostJson(LoanlyNetDefine.BASE_URL + "/Base/CallInterface",
    // XmlAndJsonUtil.beanToJson(bean), listener, errorListener);
    // }
    //
    // public RequestQueue getRequestQueue() {
    // return requestQueue;
    // }
    //
    //
    //
    // public void cancelPost(String tag) {
    // if (requestQueue != null) {
    // Log.e("HTTPMANAGE", "CALDEL :" + tag);
    // requestQueue.cancelAll(tag);
    // if (requestMap.get(tag) != null
    // && requestMap.get(tag).get() != null) {
    // Request<String> r = requestMap.get(tag).get();
    // if (r instanceof MultipartRequest) {
    // MultipartRequest request = (MultipartRequest) r;
    // request.setCancel(true);
    // } else {
    // LoanlyRequest request = (LoanlyRequest) r;
    // request.setCancel(true);
    // }
    // requestMap.remove(tag);
    // }
    // }
    // }
    //
    // public void cancelAll() {
    // if (requestMap != null && requestMap.size() > 0) {
    // HashMap<String, SoftReference<Request<String>>> cacheMap = new
    // HashMap<String, SoftReference<Request<String>>>(
    // requestMap.size());
    // cacheMap.putAll(requestMap);
    // Iterator<Entry<String, SoftReference<Request<String>>>> iter = cacheMap
    // .entrySet().iterator();
    // while (iter.hasNext()) {
    // Map.Entry<String, SoftReference<Request<String>>> entry =
    // (Map.Entry<String, SoftReference<Request<String>>>) iter
    // .next();
    // if (!entry.getKey().equals(
    // LoanlyNetDefine
    // .URL4Request(LoanlyNetDefine.URL_OF_LOGINOUT))) {
    // cancelPost(entry.getKey());
    // }
    // }
    // cacheMap.clear();
    // cacheMap = null;
    // }
    // }
    //
    // public RetryPolicy getRetryPolicy() {
    // return retryPolicy;
    // }
    //
    // public void setRetryPolicy(RetryPolicy policy) {
    // retryPolicy = policy;
    // }
}
